<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.data.ConnData" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建题目</title>
    <link rel="stylesheet" href="../css/bootstrap.css">
    <script src="../js/jquery.js"></script>
    <script src="../js/bootstrap.js"></script>

    <style>
        /* 整体页面布局 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
        }


        /* 卡片样式 */
        form {
            background-color: white;
            padding: 30px;
            border-radius: 15px; /* 增大圆角半径，更圆润 */
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 调整阴影效果，更柔和 */
            width: 600px; /* 适当增大卡片宽度，可按需调整 */
            margin: 30px auto; /* 让卡片在水平方向居中 */
        }

        /* 标签样式 */
        label {
            display: block;
            font-size: 16px; /* 增大标签字体大小 */
            margin-bottom: 8px;
            color: #333; /* 标签文字颜色，深灰色 */
        }

        /* 输入框和文本域通用样式 */
        input[type="text"],
        textarea {
            width: 100%;
            padding: 12px;
            margin-bottom: 20px;
            border: 1px solid #ccc; /* 浅灰色边框 */
            border-radius: 8px;
            font-size: 14px; /* 输入框文字大小 */
            outline: none; /* 去除输入框获取焦点时的默认外边框 */
            transition: border-color 0.3s ease; /* 边框颜色过渡效果 */
        }

        input[type="text"]:focus,
        textarea:focus {
            border-color: #007bff; /* 输入框获取焦点时边框颜色变化 */
        }

        /* 按钮通用样式 */
        input[type="submit"] {
            display: inline-block; /* 让按钮以行内块元素显示，便于排版 */
            text-align: center;
            text-decoration: none; /* 去除链接默认下划线 */
            font-size: 16px; /* 按钮文字大小 */
            border-radius: 8px;
            cursor: pointer;
            padding: 12px 25px;
            transition: background-color 0.3s ease; /* 背景颜色过渡效果 */
            background-color: #4CAF50; /* 设置为浅绿色 */
            color: white;
            border: none;
            margin-right: 10px; /* 与取消按钮（若有）之间增加一点间距 */
        }

        input[type="submit"]:hover {
            background-color: #45a049; /* 鼠标悬停时按钮背景色稍深一点 */
        }

    </style>
</head>

<body>
<!-- 导航栏 -->
<ul class="nav nav-pills" style="padding-top:20px">
    <li class="nav-item" style="padding-left:250px">
        <a class="nav-link" style="color:black" href="admin.jsp">管理</a>
    </li>
    <li class="nav-item" style="padding-left:250px">
        <a class="nav-link active"  href="create_question.jsp">创建</a>
    </li>
    <li class="nav-item" style="padding-left:250px">
        <a class="nav-link " style="color:black"  href="question_modify_detail.jsp">修改</a>
    </li>
    <li class="nav-item" style="padding-left:250px">
        <a class="nav-link" style="color:grey" href="../index.jsp">退出</a>
    </li>
</ul>
<hr>

<!-- 卡片内容 -->
<div>
    <%
        // 用于存储创建题目操作的结果信息
        String resultMessage = "";
        // 判断是否是表单提交后的处理
        if ("POST".equals(request.getMethod())) {
            request.setCharacterEncoding("utf-8");
            String questionName = request.getParameter("question_name");
            String questionContent = request.getParameter("question_content");
            String sampleInput = request.getParameter("sample_input");
            String sampleOutput = request.getParameter("sample_output");
            String testCaseInput = request.getParameter("test_case_input");
            String testCaseOutput = request.getParameter("test_case_output");

            ConnData objdata = new ConnData();
            Connection connection = null;
            PreparedStatement psmt = null;
            try {
                connection = objdata.getConn();
                String sql = "INSERT INTO question_table (question_name, question_content, sample_input, sample_output, test_case_input, test_case_output) VALUES (?,?,?,?,?,?)";
                psmt = objdata.getPsmt(sql);
                psmt.setString(1, questionName);
                psmt.setString(2, questionContent);
                psmt.setString(3, sampleInput);
                psmt.setString(4, sampleOutput);
                psmt.setString(5, testCaseInput);
                psmt.setString(6, testCaseOutput);
                int rowsAffected = psmt.executeUpdate();
                if (rowsAffected > 0) {
                    resultMessage = "题目创建成功！";
                } else {
                    resultMessage = "题目创建失败，请稍后再试。";
                }
            } catch (SQLException e) {
                e.printStackTrace();
                resultMessage = "数据库操作出现错误，请稍后再试！";
            } finally {
                if (psmt!= null) {
                    try {
                        psmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (connection!= null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    %>
    <% if (!"".equals(resultMessage)) { %>
    <p><%=resultMessage%></p>
    <% } %>
    <form action="create_question.jsp" method="post">
        <label for="question_name">题目名称：</label>
        <input type="text" id="question_name" name="question_name" required><br>
        <label for="question_content">题目内容：</label>
        <textarea id="question_content" name="question_content" required></textarea><br>
        <label for="sample_input">样例输入：</label>
        <textarea id="sample_input" name="sample_input"></textarea><br>
        <label for="sample_output">样例输出：</label>
        <textarea id="sample_output" name="sample_output"></textarea><br>
        <label for="test_case_input">测试用例输入：</label>
        <textarea id="test_case_input" name="test_case_input"></textarea><br>
        <label for="test_case_output">测试用例输出：</label>
        <textarea id="test_case_output" name="test_case_output"></textarea><br>
        <input type="submit" value="创建题目">
    </form>
</div>
</body>

</html>